package com.sanjiahantang.youjiaclient.module.store.preseneters;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.module.common.views.HomeView;
import com.sanjiahantang.youjiaclient.module.store.beans.StoreBean;
import com.sanjiahantang.youjiaclient.module.store.models.IStoreModel;
import com.sanjiahantang.youjiaclient.module.store.models.impl.StoreModel;
import com.sanjiahantang.youjiaclient.module.store.views.StoreView;
import com.sanjiahantang.youjiaclient.module.store.views.impl.StoreDetailActivity;
import com.sanjiahantang.youjiaclient.module.store.views.impl.StoreOnMapActivity;
import com.sanjiahantang.youjiaclient.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.DEFAULT_LAT;
import static com.sanjiahantang.youjiaclient.config.StaticValue.DEFAULT_LON;
import static com.sanjiahantang.youjiaclient.config.StaticValue.DEFAULT_PAGE_SIZE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_LAT;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_LON;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PAGE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PAGE_SIZE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PARAM;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TITLE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TOKEN;
import static com.sanjiahantang.youjiaclient.config.StaticValue.RESPONSE_CODE_SUC;

/**
 * Created by zy on 2017/9/20.
 */

public class StorePresenter extends BaseMvpPresenter<StoreView> implements CallBack<StoreBean> {
    private StoreView.OnLoadListener onLoadListener;
    private IStoreModel storeModel;
    private QuickAdapter<StoreBean.Item> storeAdapter;

    public StorePresenter() {
        storeModel = new StoreModel();
    }

    public void loadData(Context context, StoreView.OnLoadListener onLoadListener) {
        this.onLoadListener = onLoadListener;
        if (mView == null) return;
        mView.showProgress();
        Map<String, Object> param = new HashMap<>();
        if (!TextUtils.isEmpty(ConfigValue.DATA_KEY)) {
            param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        }
        param.put(KEY_PAGE, 0);
        param.put(KEY_PAGE_SIZE, DEFAULT_PAGE_SIZE);
        Map<String, Object> map = new HashMap<>();
        map.put(KEY_LAT, ConfigValue.lat == 0 ? DEFAULT_LAT : ConfigValue.lat);
        map.put(KEY_LON, ConfigValue.lon == 0 ? DEFAULT_LON : ConfigValue.lon);
        map.put(HomeView.KEY_ADDRESS, ConfigValue.city != null ? ConfigValue.city : HomeView.DEFAULT_CITY);
        param.put(KEY_PARAM, map);

        storeModel.loadStoreList(context, param, this);

        if (storeAdapter == null) {
            storeAdapter = new QuickAdapter<StoreBean.Item>(context, R.layout.item_store) {
                @Override
                protected void convert(BaseAdapterHelper helper, final StoreBean.Item item) {
                    helper.setText(R.id.nearby_shop_name, item.getExperienceName());
                    helper.setText(R.id.nearby_address, (item.getExperienceAddress() == null ? "" : item.getExperienceAddress())
                            + (item.getExperienceAddressDetail() == null ? "" : item.getExperienceAddressDetail()));
                    helper.setText(R.id.nearby_distance, String.format("离你%.2f", item.getDistance()));
                    Picasso.with(context).load(FileUtils.rePath(item.getUrl())).resize(150,150).placeholder(R.drawable.i_match).into((ImageView) helper.getView(R.id.nearby_shop_img));
                    helper.getView().setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                            context.startActivity(new Intent(context,StoreOnMapActivity.class)
//                                    .putExtra(KEY_TITLE,item.getExperienceName())
//                                    .putExtra(KEY_LAT,item.getLat())
//                                    .putExtra(KEY_LON,item.getLon())
//                                    );
                            context.startActivity(new Intent(context, StoreDetailActivity.class)
                                    .putExtra(KEY_ROLEID, item.getRoleId())

                            );
                        }
                    });
                    helper.setOnClickListener(R.id.ivCall, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Utils.call(context, item.getLinkPersonPhone());
                        }
                    });

                    LinearLayout llDevice = helper.getView(R.id.llDevice);
                    if (item.getIconList() != null) {
                        for (int i = 0, max = item.getIconList().size(); i < max; i++) {
                            ImageView imageView = new ImageView(context);
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Utils.dip2px(context, 15), Utils.dip2px(context, 15));
                            imageView.setLayoutParams(params);
                            StoreBean.Icon icon = item.getIconList().get(i);
                            if (icon != null)
                                Picasso.with(context).load(FileUtils.rePath(icon.getUrl())).into(imageView);
                            llDevice.addView(imageView);
                        }
                    }
                }
            };
        }
    }

    @Override
    public void onSuccess(StoreBean response, Context context, Object tag) {
        if (mView == null) return;
        mView.hideProgress();

        if (response.getCode() == RESPONSE_CODE_SUC && response.getData().getList() != null) {
            storeAdapter.addAll(response.getData().getList());
        }


        onLoadListener.initListView(storeAdapter);
    }

    @Override
    public void onFailure(String message) {
        if (mView == null) return;
        mView.hideProgress();
    }
}
